package com.yystudy.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yystudy.common.result.Result;
import com.yystudy.model.system.SysUser;
import com.yystudy.model.vo.SysUserQueryVo;
import com.yystudy.system.annotation.Log;
import com.yystudy.system.enums.BusinessType;
import com.yystudy.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yystudy
 * @since 2023-10-12
 */
@RestController
@Api(tags = "用户管理接口")
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    /**
     * 用户分页条件查询
     *
     * @param page
     * @param limit
     * @param sysUserQueryVo
     * @return
     */
    @ApiOperation(value = "用户分页条件查询")
    @GetMapping("/{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    public Result list(
            @PathVariable Long page,
            @PathVariable Long limit,
            SysUserQueryVo sysUserQueryVo) {

        //创建page分页对象
        Page<SysUser> pageParam = new Page<>(page, limit);
        IPage<SysUser> pageResult = sysUserService.selectPage(pageParam, sysUserQueryVo);
        return Result.ok(pageResult);
    }


    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Log(title = "用户管理",businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加用户")
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser) {
        String md5 = DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes());
        sysUser.setPassword(md5);
        boolean save = sysUserService.save(sysUser);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /**
     * 根据id进行查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id进行查询")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @GetMapping("/findById/{id}")
    public Result getUserById(@PathVariable Long id) {
        SysUser byId = sysUserService.getById(id);
        return Result.ok(byId);
    }


    /**
     * 修改用户
     *
     * @param sysUser
     * @return
     */
    @Log(title = "用户管理",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody SysUser sysUser) {
        boolean isSuccess = sysUserService.updateById(sysUser);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @Log(title = "用户管理",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean isSuccess = sysUserService.removeById(id);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @Log(title = "用户管理",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "批量删除用户")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> ids) {
        boolean isSuccess = sysUserService.removeByIds(ids);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /**
     * 更改用户状态
     * @param id
     * @param status
     * @return
     */
    @Log(title = "用户管理",businessType = BusinessType.STATUS)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更改用户状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatusById(@PathVariable Long id,
                                   @PathVariable Integer status){
        sysUserService.updateStatusById(id,status);
        return Result.ok();
    }



}
